using System;
using System.IO;
using System.Text;


namespace ThalesHsmComms
{
	public class TranslatePinBdk2ZpkMsg : WSMMsg
	{

		private String destPinBlock;
		private String checkDigits;
		
		private String bdkSrc;
		private String zpkDest;
		private String srcPinBlock;
	    private String pan;
	    private String ksn;
	    private String ksnDescriptor;

		public TranslatePinBdk2ZpkMsg(String bdk_src, String zpk_dest, String src_pin_block, String pan, String ksn, String ksn_descriptor)
			:base("CI")
		{
			this.bdkSrc = bdk_src;
			this.zpkDest = "U" + zpk_dest;
			this.srcPinBlock = src_pin_block;
	        this.pan = pan;
	        this.ksn = ksn;
	        this.ksnDescriptor = ksn_descriptor;			
		}

		override protected void Encode()
		{
			StringBuilder dataWriter = new StringBuilder();
			
			try {
			
				// pack command code
				dataWriter.Append(this.commandCode);
			
				// pack source bdk
				dataWriter.Append(this.bdkSrc);
			
				// pack destination zpk 
				dataWriter.Append(this.zpkDest);
	
				// pack ksn descriptor
				dataWriter.Append(this.ksnDescriptor);
	
				// pack ksn
				dataWriter.Append(this.ksn);
	
				// pack pin block to translate
				dataWriter.Append(this.srcPinBlock);
				
				// set destination pin block format ISO-0
				dataWriter.Append("01");

	
				
	            // set 12 right-most digits of account number excluding check digit
	            //dataWriter.Write(Encoding.ASCII.GetBytes(this.pan.Substring(pan.Length - 13, pan.Length - 1)));
				dataWriter.Append(this.pan.Substring(pan.Length - 13, 11));
							
				this.requestData = ASCIIEncoding.ASCII.GetBytes(dataWriter.ToString());
			
			} catch (IOException e) {
				Console.WriteLine(e);
			}

		}
	}
}

